package com.lelefly.base.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lelefly.base.model.Result;
import com.lelefly.base.model.RowResult;
import com.lelefly.base.model.SearchInsuranceBean;
import com.lelefly.base.po.LeleflyCompanyInsurance;
import com.lelefly.base.po.LeleflyPersonalInsurance;
import com.lelefly.base.serviceImp.InsuranceService;
import com.lelefly.constant.Constant;

@Controller
@RequestMapping("/insurance")
public class InsuranceController {

	@Resource
	private InsuranceService insuranceService;
	
	/**
	 * 添加个人保单
	 * @param personalInsurance
	 */
	@RequestMapping(value="/add_personal_insurance",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result addPersonalInsurance(LeleflyPersonalInsurance personalInsurance){
		
		Result result = new Result<>();
		if (personalInsurance == null) {
			result.setMes("数据错误");
			result.setState(Constant.HTTP_STATUS_FAIL);
			return result;
		}
		
		if (StringUtils.isEmpty(personalInsurance.getUsername())
				|| StringUtils.isEmpty(personalInsurance.getPhonenumber())
				|| StringUtils.isEmpty(personalInsurance.getIdno())
				|| StringUtils.isEmpty(personalInsurance.getFlyerName())
				|| StringUtils.isEmpty(personalInsurance.getFlyerIdNo())
				|| StringUtils.isEmpty(personalInsurance.getPlaneId())) {
			result.setMes("必输项不能为空！");
			result.setState(Constant.HTTP_STATUS_FAIL);
			return result;
		}
		
		result = insuranceService.addPersonalInsurance(personalInsurance);
		return result;
	}
	
	
	/**
	 * 添加企业保单
	 * @param personalInsurance
	 */
	@RequestMapping(value="/add_company_insurance",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result addCompanyInsurance(LeleflyCompanyInsurance companyInsurance){
		
		Result result = new Result<>();
		if (companyInsurance == null) {
			result.setMes("映射错误");
			result.setState(Constant.HTTP_STATUS_FAIL);
			return result;
		}
		
		if (StringUtils.isEmpty(companyInsurance.getUsername())
				|| StringUtils.isEmpty(companyInsurance.getPhonenumber())
				|| StringUtils.isEmpty(companyInsurance.getIdno())
				|| StringUtils.isEmpty(companyInsurance.getFlyerName())
				|| StringUtils.isEmpty(companyInsurance.getFlyerIdNo())
				|| StringUtils.isEmpty(companyInsurance.getCompanyname())
				|| StringUtils.isEmpty(companyInsurance.getSocialCreditCode())) {
			result.setMes("必输项不能为空！");
			result.setState(Constant.HTTP_STATUS_FAIL);
			return result;
		}
		
		result = insuranceService.addCompanyInsurance(companyInsurance);
		return result;
	}
	
	@RequestMapping(value="/get_insurance_info",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result<SearchInsuranceBean> getInsuranceInfo(@RequestBody String phoneNo){
		
		Result<SearchInsuranceBean> result = new Result<>();
		if (StringUtils.isEmpty(phoneNo)) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("手机号不能为空");
			return result;
		}
		
		result = insuranceService.getInsuranceInfo(phoneNo);
		return result;
	}
	
	@RequestMapping(value="/query_insurance_info")
	@ResponseBody
	public Result<SearchInsuranceBean> queryInsurance(@RequestBody String planeId){
		Result<SearchInsuranceBean> result = new Result<>();
		if (StringUtils.isEmpty(planeId)) {
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("飞机ID不能为空");
			return result;
		}
		result = insuranceService.queryInsuranceInfo(planeId);
		return result;
	}
	@RequestMapping("/query_all_insurance")
	@ResponseBody
	public RowResult<LeleflyCompanyInsurance> queryAllInsurance(HttpServletRequest request){
		RowResult<LeleflyCompanyInsurance> result = new RowResult<>();
		
		String limit = request.getParameter("limit");
		String start = request.getParameter("start");
		result = insuranceService.queryAllInsurance(limit,start);
		return result;
	}
}
